package hu.bme.mit.Grafilogika.client;

import java.util.LinkedList;

import javax.ejb.EJB;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import hu.bme.mit.Grafilogika.GrafilogikaBeanLocal;
import hu.bme.mit.Grafilogika.business.NTableManagerLocal;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GrafilogikaGWT implements EntryPoint {
	
	private final CreateNonogramServiceAsync createNonogramService = GWT.create(CreateNonogramService.class);
	
	  private FlexTable nonogramTable = new FlexTable();
	  private FlexTable rowNumbers = new FlexTable();
	  private FlexTable columnNumbers = new FlexTable();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
//		final NTableManagerLocal nTableManager = new NTableManager();
        final Button generateButton = new Button("Generate");
        final Button nonogramButton = new Button("CreateNonogram");
        final TextBox rowField = new TextBox();
        rowField.setText("1");
        final TextBox columnField = new TextBox();
        columnField.setText("1");
        final Label errorLabel = new Label();
        VerticalPanel vertical = new VerticalPanel();
        HorizontalPanel horizontal1 = new HorizontalPanel();
        HorizontalPanel horizontal2 = new HorizontalPanel();
        vertical.add(horizontal2);
        vertical.add(horizontal1);
        horizontal1.add(rowNumbers);
        horizontal1.add(nonogramTable);
        Label lab = new Label();
        lab.setWidth("37px");
        horizontal2.add(lab);
        horizontal2.add(columnNumbers);
        // We can add style names to widgets
        generateButton.addStyleName("sendButton");
        
        // Add the nameField and sendButton to the RootPanel
        // Use RootPanel.get() to get the entire body element
        RootPanel.get("rowFieldContainer").add(rowField);
        RootPanel.get("columnFieldContainer").add(columnField);
        RootPanel.get("generateButtonContainer").add(generateButton);
        RootPanel.get("errorLabelContainer").add(errorLabel);
        RootPanel.get("generateNonogramContainer").add(nonogramButton);

        generateButton.addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		        generateTable(Integer.parseInt(rowField.getText()), Integer.parseInt(columnField.getText()));
		      }
		    });	

		// Associate the Main panel with the HTML host page.
		RootPanel.get("nonogramTable").add(vertical);
		 
		nonogramButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				String s1 = "1011";
				String s2 = "1011";
				String s3 = "1011";
				String s4 = "1011";
				LinkedList<String> list = new LinkedList<String>();
				list.add(s1); list.add(s2); list.add(s3); list.add(s4);
				createNonogramService.createNonogram(list,new AsyncCallback<Long>() {
					
					@Override
					public void onSuccess(Long result) {
						errorLabel.setText("+");
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						errorLabel.setText("-");
						
					}
				});
//				nTableManager.createNonogram(list);
//				errorLabel.setText("Nonogram Created!");
			}
		});

    // Listen for keyboard events in the input box.
	    columnField.addKeyPressHandler(new KeyPressHandler() {
	    	public void onKeyPress(KeyPressEvent event) {
	    		if (event.getCharCode() == KeyCodes.KEY_ENTER) {
	    			generateTable(Integer.parseInt(rowField.getText()), Integer.parseInt(columnField.getText()));
	    		}
	    	}
	    });
	    rowField.addKeyPressHandler(new KeyPressHandler() {
	    	public void onKeyPress(KeyPressEvent event) {
	    		if (event.getCharCode() == KeyCodes.KEY_ENTER) {
	    			generateTable(Integer.parseInt(rowField.getText()), Integer.parseInt(columnField.getText()));
	    			
	    			
	    			//NTableManager a = new NTableManager();
	    		}
	    	}
	    });
	}
	private void generateTable(int width, int height){
		nonogramTable.removeAllRows();
		rowNumbers.removeAllRows();
		columnNumbers.removeAllRows();
//		rowNumbers.setBorderWidth(2);
//		columnNumbers.setBorderWidth(2);
		nonogramTable.setBorderWidth(2);
		for (int i = 0; i < width; i++){
			for (int j = 0; j < height; j++){
				final NonogramTableElement b = new NonogramTableElement();
				nonogramTable.setWidget(i, j, b);
			    b.addClickHandler(new ClickHandler() {
			    	public void onClick(ClickEvent event) {
			    		b.changeState();
				      }
				    });	
			}
		}
		for (int j = 0; j < width; j++) {
			Label l1 = new Label("2");
			Label l2 = new Label("2");
	//		l.setWidth("25px");
			l1.setHeight("27px");
			l2.setHeight("27px");
			rowNumbers.setWidget(j, 0, l1);
			rowNumbers.setWidget(j, 1, l2);
		}
		for (int i = 0; i < height; i++) {
			Label l = new Label("3");
			Label l2 = new Label("2");
			l.setWidth("27px");
			l2.setWidth("27px");
	//		l.setHeight("25px");
			columnNumbers.setWidget(0, i, l);
			columnNumbers.setWidget(1, i, l2);
		}
		Label l = new Label("3");
		Label l2 = new Label("2");
		l.setWidth("27px");
		l2.setWidth("27px");
		columnNumbers.setWidget(2, 2, l);
		columnNumbers.setWidget(3, 2, l2);
	}
}
